package it.megasoft78.turingmachine;

import java.util.ArrayList;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class FileDialogAdapter extends ArrayAdapter<FileDialogItem> {
	private Activity context;
	private ArrayList<FileDialogItem> items;

	public FileDialogAdapter(Activity context, ArrayList<FileDialogItem> items) {
		super(context, R.layout.file_dialog_item, items);
		this.context = context;
		this.items = items;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = null;
		ImageView imgIcon = null;
		TextView lblName = null;
		TextView lblSize = null;
		TextView lblLastChange = null;

		if (convertView == null) {
			LayoutInflater inflater = context.getLayoutInflater();
			view = inflater.inflate(R.layout.file_dialog_item, null, true);
			imgIcon = (ImageView) view.findViewById(R.id.imgIcon);
			lblName = (TextView) view.findViewById(R.id.lblName);
			lblSize = (TextView) view.findViewById(R.id.lblSize);
			lblLastChange = (TextView) view.findViewById(R.id.lblLastChange);
			view.setTag(new Object[] { imgIcon, lblName, lblSize, lblLastChange });
		} else {
			view = convertView;
			Object[] objects = (Object[]) view.getTag();
			imgIcon = (ImageView) objects[0];
			lblName = (TextView) objects[1];
			lblSize = (TextView) objects[2];
			lblLastChange = (TextView) objects[3];
			
		}
		
		FileDialogItem item = items.get(position);
		
		imgIcon.setImageResource(item.getIconResourceId());
		lblName.setText(item.getName());
		lblSize.setText(item.getSize());
		lblLastChange.setText(item.getLastChange());

		return view;
	}
}
